@using System.Globalization
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Http
@using Oqtane.Models
@namespace Oqtane.Themes.Controls
@inherits ThemeControlBase
@inject ILanguageService LanguageService
@inject NavigationManager NavigationManager

@if (_supportedCultures?.Count() > 1)
{
    <div class="btn-group pe-1" role="group">
        <button id="btnCultures" type="button" class="btn @ButtonClass dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="oi oi-globe"></span>
        </button>
        <div class="dropdown-menu @MenuAlignment" aria-labelledby="btnCultures">
            @foreach (var culture in _supportedCultures)
            {
                @if (PageState.RenderMode == RenderModes.Interactive)
                {
                    <a class="dropdown-item @(CultureInfo.CurrentUICulture.Name == culture.Name ? "active" : String.Empty)" href="#" @onclick="@(async e => await SetCultureAsync(culture.Name))" @onclick:preventDefault="true">@culture.DisplayName</a>
                }
                else
                {
                    <a class="dropdown-item @(CultureInfo.CurrentUICulture.Name == culture.Name ? "active" : String.Empty)" href="@NavigateUrl(PageState.Page.Path, "culture=" + culture.Name)" data-enhance-nav="false">@culture.DisplayName</a>
                }
            }
        </div>
    </div>
}

@code{
    private IEnumerable<Culture> _supportedCultures;
    private string MenuAlignment = string.Empty;

    [Parameter]
    public string DropdownAlignment { get; set; } = string.Empty; // Empty or Left or Right
    [Parameter]
    public string ButtonClass { get; set; } = "btn-outline-secondary";

    [CascadingParameter]
    HttpContext HttpContext { get; set; }

    protected override void OnParametersSet()
    {
        MenuAlignment = DropdownAlignment.ToLower() == "right" ? "dropdown-menu-end" : string.Empty;

        _supportedCultures = PageState.Languages.Select(l => new Culture { Name = l.Code, DisplayName = l.Name });

        if (PageState.QueryString.ContainsKey("culture"))
        {
            var culture = PageState.QueryString["culture"];
            if (_supportedCultures.Any(item => item.Name == culture))
            {
                var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture));

                HttpContext.Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue, new CookieOptions
                {
                    Path = "/",
                    Expires = DateTimeOffset.UtcNow.AddYears(365),
                    SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax, // Set SameSite attribute
                    Secure = true, // Ensure the cookie is only sent over HTTPS
                    HttpOnly = false // cookie is updated using JS Interop in Interactive render mode
                });

            }
            NavigationManager.NavigateTo(NavigationManager.Uri.Replace($"?culture={culture}", ""));
        }
    }

    private async Task SetCultureAsync(string culture)
    {
        if (culture != CultureInfo.CurrentUICulture.Name)
        {
            var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture));
            var interop = new Interop(JSRuntime);
            await interop.SetCookie(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue, 360, true, "Lax");
            NavigationManager.NavigateTo(NavigationManager.Uri, true);
        }
    }
}
